package com.buho29.rcmanager.bd;

import android.database.Cursor;

public class TableStatistic extends BaseActiveRecord {

	public static final String COL_TOTAL = "total";
	public static final String COL_AVG = "avg";
	public static final String COL_MIN = "min";
	public static final String COL_MAX = "max";
	public static final String COL_COUNT = "count";
	
	private int mCount;
	public int getCount() {return mCount;}
	public void setCount(int count) {mCount = count;}

	private int mAverage;
	public int getAverage() {return mAverage;}
	public void setAverage(int average) {mAverage = average;}

	private int mTotal;
	public int getTotal() {return mTotal;}
	public void setMTotal(int total) {mTotal = total;}

	private int mMax;
	public int getMax() {return mMax;}
	public void setMax(int max) {mMax = max;}

	private int mMin;
	public int getMin() {return mMin;}
	public void setMin(int min) {mMin = min;}

	
	@Override
	public boolean delete() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Boolean load() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Cursor retrieveAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long save() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	// count , total , avg , min , max 
	public Cursor fetchPowerItem(long powerItemID) {
		Cursor tmp = mDb.rawQuery(
				"SELECT Count(*) AS count, Sum(drive.time) AS total, Avg(drive.time) AS avg, "+
				"Min(drive.time) AS min, Max(drive.time) AS max "+
				"FROM drive WHERE powerItem_id=?",
				new String[]{String.valueOf(powerItemID)}
			);
		tmp.moveToFirst();
		return tmp;
	}
	
	public void loadPowerItem(long powerItemID) {
		loadCursor(fetchPowerItem(powerItemID));
	}
	
	
	// count , total , avg , min , max 
	public Cursor fetchPower(long powerItemID ,long modelID) {
		Cursor tmp = mDb.rawQuery(
			"SELECT Count(*) AS count, Sum(drive.time) AS total, Avg(drive.time) AS avg, "+
			"Min(drive.time) AS min, Max(drive.time) AS max "+
			"FROM drive WHERE drive.model_id=? AND drive.powerItem_id=?", // cagada
			new String[]{String.valueOf(modelID),String.valueOf(powerItemID)}
		);
		tmp.moveToFirst();
		return tmp;
	}
	
	public void loadPower(long powerID,long modelID) {
		loadCursor(fetchPower(powerID,modelID));
	}

		
	// count , total , avg , min , max 
	public Cursor fetchModel(long modelID) {
		Cursor tmp = mDb.rawQuery(
			"SELECT Count(*) AS count, Sum(drive.time) AS total, Avg(drive.time) AS avg, "+
			"Min(drive.time) AS min, Max(drive.time) AS max "+
			"FROM drive WHERE drive.model_id=?",
			new String[]{String.valueOf(modelID)}
		);
		tmp.moveToFirst();
		return tmp;
	}
	
	public void loadModel(long modelID) {
		loadCursor(fetchModel(modelID));
	}
	
	public Cursor fechDamage(long modelID) {
		Cursor tmp = mDb.rawQuery(
			"SELECT Sum(damage.price*damage.count) AS total " +
			"FROM damage WHERE damage.model_id=?",
			new String[]{String.valueOf(modelID)}
		);
		tmp.moveToFirst();
		return tmp;
	}
	
	public void loadDamage(long modelID) {
		Cursor st = fechDamage(modelID);
    	mTotal = st.getInt(st.getColumnIndexOrThrow(TableStatistic.COL_TOTAL));
		st.close();
	}
	
	public void loadCursor(Cursor st){
		mCount = st.getInt(st.getColumnIndexOrThrow(TableStatistic.COL_COUNT));
    	mAverage = st.getInt(st.getColumnIndexOrThrow(TableStatistic.COL_AVG));
    	mTotal = st.getInt(st.getColumnIndexOrThrow(TableStatistic.COL_TOTAL));
    	mMax = st.getInt(st.getColumnIndexOrThrow(TableStatistic.COL_MAX));
    	mMin = st.getInt(st.getColumnIndexOrThrow(TableStatistic.COL_MIN));  
		st.close();
	}
}
